import {
  screen,
  mouse,
  centerOf,
  straightTo,
  Button,
  linear,
  keyboard,
  clipboard,
  Key,
  Point,
  RGBA,
  pixelWithColor,
  Region,
} from "@nut-tree/nut-js";
import "@nut-tree/template-matcher";

// 消息菜单
export const MESSAGE_MENU = new Region(12, 65, 130, 28); // windows
// export const MESSAGE_MENU = new Region(65, 103, 134, 26) // mac

// 聊天窗
export const CHAT_WINDOW = new Region(425, 70, 821, 530); // windows
// export const CHAT_WINDOW = new Region(477, 105, 937, 627) // mac

export const CHAT_TITLE = new Region(410, 28, 42, 20) // windows

// 菜单栏面板
export const MENU_PANEL = new Region(0, 0, 156, 720); // windows

// 会话面板
export const SESSION_PANEL = new Region(164, 0, 236, 720); // windows

// 聊天面板
export const CHAT_PANEL = new Region(425, 0, 821, 720); // windows

// 未读菜单
export const UNREAD_MENU = new Region(12, 393, 130, 28); // windows

// 未读联系人
export const UNREAD_CONTACT_FIRST = new Region(164, 66, 236, 58); // windows

// 2、未读消息
export default async function unreadMessage() {
  const match = await screen.findAll(
    pixelWithColor(new RGBA(252, 90, 78, 255)),
    {
      searchRegion: MENU_PANEL,
    }
  );
  if (match && match.length) {
    // 1. 点击“未读”按钮
    // @ts-ignore
    await mouse.move(straightTo(centerOf(MESSAGE_MENU)), linear(100))
    await mouse.move(straightTo(centerOf(UNREAD_MENU)), linear(100));
    await mouse.click(Button.LEFT);

    // 2. 点击第一个未读联系人
    // @ts-ignore
    await mouse.move(straightTo(centerOf(UNREAD_CONTACT_FIRST)), linear(100));
    await mouse.click(Button.LEFT);
    
    // 3. 拷贝聊天窗口标题
    // @ts-ignore
    await mouse.move(straightTo(centerOf(CHAT_TITLE)), linear(50))
    await mouse.click(Button.RIGHT)
    
    await mouse.move(
      straightTo(
        new Point(CHAT_TITLE.left + CHAT_TITLE.width, CHAT_TITLE.top + CHAT_TITLE.height)
      ), // @ts-ignore
      linear(50)
    )
    await mouse.click(Button.LEFT)

    const name = await clipboard.getContent()
    console.log("name:", name);
    
    // 4. 拷贝聊天内容
    const copiedText = await getMessage();
    return copiedText;
  }
}

export async function getMessage() {
  // @ts-ignore
  await mouse.move(
    straightTo(new Point(CHAT_WINDOW.left, CHAT_WINDOW.top)),
    linear(100)
  );
  await mouse.pressButton(Button.LEFT);
  await mouse.move(
    straightTo(
      new Point(
        CHAT_WINDOW.left + CHAT_WINDOW.width,
        CHAT_WINDOW.top + CHAT_WINDOW.height
      )
    ), // @ts-ignore
    linear(100)
  );
  await mouse.releaseButton(Button.LEFT);

  await keyboard.pressKey(Key.LeftControl, Key.C);
  await keyboard.releaseKey(Key.LeftControl, Key.C);

  const copiedText = await clipboard.getContent();
  return copiedText;
}

unreadMessage();
